#include "rtda/heap/cp_symref.h"
#include "rtda/heap/constant_pool.hpp"
#include "rtda/heap/class_loader.h"
#include "rtda/heap/class.h"
#include <stdexcept>

namespace jvm {
namespace heap {

SymRef::SymRef(std::shared_ptr<ConstantPool> pCp) : m_pCp(pCp), m_pClass(nullptr) {}

std::shared_ptr<Class> SymRef::resolvedClass() {
    if (!m_pClass) {
        resolveClassRef();
    }
    return m_pClass;
}

void SymRef::resolveClassRef() {
    std::shared_ptr<Class> pD = m_pCp->getClass();
    std::shared_ptr<Class> pC = pD->getLoder()->loadClass(m_className);
    
    if (!pC->isAccessibleTo(pD)) {
        throw std::runtime_error("java.lang.IllegalAccessError");
    }
    
    m_pClass = pC;
}

} // namespace heap
} // namespace jvm